<?php
/**
 * Created by PhpStorm.
 * User: My
 * Date: 2018/11/19
 * Time: 11:44
 */

namespace App\Repositories;

use App\Http\Helper\BeanHelper;
use App\Models\TestingSyncOrder;

class TestingSyncOrderRepository extends Repository
{
    /**
     * 根据ID获取用户的基本信息
     * @param $uid
     * @return mixed
     */
    public function getOneByUid($uid) {
        return BeanHelper::convertStdClsToArr(
            TestingSyncOrder::where('uid', $uid)->orderBy('id', 'desc')->first()
        );
    }

    /**
     * 保存用户的基本信息
     * @param $uid
     * @param $phone
     * @param $orderId
     * @param $nutritionistId
     * @param $status
     * @return bool|mixed
     */
    public function save($uid, $phone, $orderId, $nutritionistId, $status) {
        if ($userInfo = $this->getOneByUid($uid)) {
            if ($userInfo['phone'] != $phone) {
                $userInfo['order_id'] = $orderId;
                $userInfo['nutritionist_id'] = $nutritionistId;
                $userInfo['status'] = $status;
                return $this->update($userInfo);
            }
            return true;
        } else {
            $userInfo = [
                'uid' => $uid,
                'phone' => $phone,
                'order_id' => $orderId,
                'nutritionist_id' => $nutritionistId,
                'status' => $status,
            ];
            return $this->store($userInfo);
        }
    }

    /**
     * 添加用户数据
     * @param $data
     * @return mixed
     */
    public function store($data) {
        return TestingSyncOrder::firstOrCreate($data);
    }

    /**
     * 根据uid修改测评同步数据
     * @param $uid
     * @param $data
     * @return mixed
     */
    public function updateByUid($uid, $data) {
        $data['update_time'] = time();
        return TestingSyncOrder::where('uid', $uid)->update($data);
    }

    /**
     * 修改测评同步数据
     * @param $data
     * @return mixed
     */
    public function update($data) {
        $data['update_time'] = time();
        return TestingSyncOrder::where('id', $data['id'])->update($data);
    }

    public function model()
    {
        // TODO: Implement model() method.
    }

    public function tag()
    {
        // TODO: Implement tag() method.
    }
}